const { Fans } = require('../model/fans')
const { User } = require('../model/users')

// 关注
const follow = async ctx => {
    try {
        let fans = ctx.request.body
        const rel = await Fans.create(fans)
        if(rel) {
            ctx.body = {
                code: 200,
                msg: '关注成功'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '关注失败'
            }
        }
    }
    catch (error) {
        ctx.body = {
            code: 500,
            msg: error
        }
    }
}

// 取消关注
const unfollow = async ctx => {
    try {
        let { username, author } = ctx.request.body
        const rel = await Fans.findOneAndDelete({ username, author })
        if(rel) {
            ctx.body = {
                code: 200,
                msg: '取消关注成功'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '取消关注失败'
            }
        }
    }
    catch (error) {
        ctx.body = {
            code: 500,
            msg: error
        }
    }
}

module.exports = {
    follow,
    unfollow
}
